<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
<script>

//遵循FIFO原则
	function Queue(){
		this.items=[];
		//添加元素
		this.enqueue=function(element){
			this.items.push(element)
		};
		//移除元素
		this.dequeue=function(){
			return this.items.shift();
		};

		//返回最前面的元素
		this.front=function(){
			return this.items[0];
		};

		//判断数组是不是空
		this.isEmpty=function(){
			return this.items.length==0;
		};

		//清空数组
		this.clear=function(){
			this.items=[];
		};
		//返回数组的长度
		this.size=function(){
			return this.items.length;
		};

		//打印数组
		this.print=function(){
			console.log(this.items.toString());
		}

	}

	//循环队列--击鼓传花
	function hotPotato(nameList,num){
		var queue=new Queue();
		for(var i=0;i<nameList.length;i++){
			queue.enqueue(nameList[i]);
		}
		var eliminated='';
		while(queue.size() >1){
			for(var i=0;i<num;i++){
				queue.enqueue(queue.dequeue());
			}
			eliminated=queue.dequeue();
			console.log(eliminated+'在击鼓传花游戏中被淘汰');
		}

		return queue.dequeue();
	}

	var names=['john','jack','camila','ingrid','carl'];
	var winner=hotPotato(names,7);
	console.log('胜利者是'+winner);

</script>
</html>